
package de.swta.freezay.user.orderPanel;

import de.swta.freezay.database.dbEntities.Item;
import de.swta.freezay.database.dbEntities.ItemPackage;
import de.swta.freezay.database.dbEntities.Package;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.NumberFormatter;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;

/**
 * A simplyfied package view
 *
 * @author Christian Baeumer
 */
public final class OrderPackageView extends Panel {

    /* A NumberFormatter to display the price in the right way */
    private static final NumberFormatter NUMBER_FORMATTER
           = new NumberFormatter(new DecimalFormat("####.##"));


    /**
     * Constructor
     *
     * @param id the markupID
     * @param p the package to display
     */
    public OrderPackageView(String id, Package p) {
        super (id);

        /* init this view */
        initPackageView(p);
    }


    private final void initPackageView(Package p) {

        /* the package must be wrapped in a liste because a list view is used
         * to display the package data */
        List<Package> packageList = new ArrayList<Package>();
        packageList.add(p);

        /* use a List view to display the package */
        add(new ListView<Package>("package", packageList) {

            @Override
            protected void populateItem(final ListItem<Package> item) {

                final Package p = item.getModelObject();

                item.add(new Label("Name", p.getName()));
                item.add(new MultiLineLabel("Description", ""
                        + p.getDescription()));
                item.add(new Label("Category", p.getCategory().getName()));

                /* use helper to add the package items */
                OrderPackageView.this.addItems(p.getItemPackageCollection(),
                        item);

                /* format the price and add it */
                try {
                    item.add(new Label("Price", ""
                            + NUMBER_FORMATTER.valueToString(p.getPrice()) +
                            " €"));
                } catch (ParseException ex) {
                    Logger.getLogger(OrderPackageView.class.getName()).log(
                            Level.SEVERE, null, ex);
                }

                /* to display the rows in different background colors the
                 * "row-attribute" must be modified */
                item.add(new AttributeModifier("class", true,
                        new AbstractReadOnlyModel<String>() {
                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        });
    }

    /**
     * Adds the items of the specified itemPackageList to the package view
     *
     * @param itemList the itemPackageList
     * @param item the ListItem
     */
     private final void addItems(final List<ItemPackage> itemList,
            final ListItem<Package> item) {
        // <editor-fold defaultstate="collapsed" desc="fill package view with items">
        /* receive items in this package and sort them by category */
        Collections.sort(itemList, new Comparator<ItemPackage>() {

            public int compare(ItemPackage o1, ItemPackage o2) {
                return o1.getItem().getCategory().getName().compareTo(
                        o2.getItem().getCategory().getName());
            }
        });

        item.add(new DataView<ItemPackage>("Items",
                new ListDataProvider(itemList)) {

            @Override
            protected void populateItem(
                    org.apache.wicket.markup.repeater.Item<ItemPackage> item) {

                Item i = item.getModelObject().getItem();
                item.add(new Label("Name", "" + i.getName()));
                item.add(new Label("Category",
                        i.getCategory().getName()));
                try {
                    item.add(new Label("Amount",
                        NUMBER_FORMATTER.valueToString(i.getAmount()) + " "
                        + i.getUnit().getName()));
                } catch (ParseException ex) {
                    Logger.getLogger(OrderPackageView.class.getName()).log(
                            Level.SEVERE, null, ex);
                }
                item.add(new Label("Count", ""
                        + item.getModelObject().getItemCount()));
            }
        });
        // </editor-fold>
    }
}
